package com.shihb.behavioralpatterns.commandpattern;/**
 *Description:
 *Version:1.0.0
 *@author shihb
 *@date 2020/7/25 20:33
 */
public class Invoker {
  private Command[] onCommands;
  private Command[] offCommands;
  private Command latestCommand;

  public Invoker() {
    //遥控器有5个开，5个关，一个取消按钮
    onCommands=new Command[5];
    offCommands=new Command[5];
    for (int i = 0; i < 5; i++) {
      onCommands[i]=new NoCommand();
      offCommands[i]=new NoCommand();
    }
    latestCommand=new NoCommand();
  }
  public void setCommand(int no,Command onCommand,Command offCommand ){
    onCommands[no]=onCommand;
    offCommands[no]=offCommand;
  }
  public void executeOnCommand(int no){
    onCommands[no].excute();
    latestCommand=onCommands[no];
  }
  public void executeOffCommand(int no){
    offCommands[no].excute();
    latestCommand=offCommands[no];
  }
  public void cancelCommand(){
    latestCommand.cancel();
    latestCommand=new NoCommand();
  }
}
